package com.ssps.front.rent.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssps.entity.*;
import com.ssps.front.rent.service.LessorParkingService;
import com.ssps.mapper.CommentMapper;
import com.ssps.mapper.OrdersMapper;
import com.ssps.mapper.OwnerMapper;
import com.ssps.mapper.ParkingMapper;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lg
 * @time 2020/07/23 13:37:19
 */
@Service
public class LessorParkingServiceImpl implements LessorParkingService {
    @Autowired
    ParkingMapper parkingMapper;

    @Autowired
    OrdersMapper ordersMapper;

    @Autowired
    CommentMapper commentMapper;

    @Autowired
    OwnerMapper ownerMapper;

    @Override
    public List<Parking> findAllParking(int uiid) {

        ParkingExample example = new ParkingExample ();
        ParkingExample.Criteria criteria = example.createCriteria ();
        criteria.andUiidEqualTo (uiid);
        List<Parking> parkings = parkingMapper.selectByExample (example);

        return parkings;
    }


    @Override
    public String getCode(String uphone) {

        String Url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";

        System.out.println (uphone);

        HttpClient client = new HttpClient ();
        PostMethod method = new PostMethod (Url);

        client.getParams ().setContentCharset ("GBK");
        method.setRequestHeader ("ContentType", "application/x-www-form-urlencoded;charset=GBK");

        int mobile_code = (int) ((Math.random () * 9 + 1) * 100000);

        String content = new String ("您的验证码是：" + mobile_code + "。请不要把验证码泄露给其他人。");

        NameValuePair[] data = {//提交短信
                new NameValuePair ("account", "C08809888"), //查看用户名是登录用户中心->验证码短信->产品总览->APIID
                new NameValuePair ("password", "cf10b4c7a9ee520bce41509b8a77d14a"),  //查看密码请登录用户中心->验证码短信->产品总览->APIKEY
                //new NameValuePair("password", util.Utils.StringUtil.MD5Encode("密码")),
                new NameValuePair ("mobile", uphone),
                new NameValuePair ("content", content),
        };
        method.setRequestBody (data);
        String msg = "";
        try {
            client.executeMethod (method);

            String SubmitResult = method.getResponseBodyAsString ();

            //System.out.println(SubmitResult);

            Document doc = DocumentHelper.parseText (SubmitResult);
            Element root = doc.getRootElement ();

            String code = root.elementText ("code");
            msg = root.elementText ("msg");
            String smsid = root.elementText ("smsid");

            System.out.println (code);
            System.out.println (msg);
            System.out.println (smsid);

            if ("2".equals (code)) {
                System.out.println ("短信提交成功");
            }

        } catch (HttpException e) {
            // TODO Auto-generated catch block
            e.printStackTrace ();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace ();
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace ();
        }


        return String.valueOf (mobile_code);
    }

    @Override
    public Map<String, Object> findByUiid(int uiid, Integer pageNum, Integer pageSize) {
        Map<String, Object> map = new HashMap<> ();

        ParkingExample parkingExample = new ParkingExample ();
        ParkingExample.Criteria criteria = parkingExample.createCriteria ();
        criteria.andUiidEqualTo (uiid);
        List<Parking> parkings = parkingMapper.selectByExample (parkingExample);
        List<Integer> parkIdList = new ArrayList<Integer> ();

        for (Parking id : parkings) {

            if (id.getStatus ().equals ("已出租")) {

                parkIdList.add (id.getPid ());
            }
        }


        OrdersExample ordersExample = new OrdersExample ();
        OrdersExample.Criteria criteria1 = ordersExample.createCriteria ();
        criteria1.andPidIn (parkIdList);

        PageHelper.startPage (pageNum, pageSize);

        List<Orders> ordersList = ordersMapper.selectByExample (ordersExample);

        PageInfo<Orders> pageInfo = new PageInfo<> (ordersList);


        map.put ("ordersList", ordersList);
        map.put ("pageInfo", pageInfo);

        return map;
    }

    @Override
    public void uploadPackage(String url, Integer uiid) {

        OwnerExample ownerExample=new OwnerExample ();
        OwnerExample.Criteria criteria = ownerExample.createCriteria ();
         criteria.andUiidEqualTo (uiid);

        List<Owner> ownerList = ownerMapper.selectByExample (ownerExample);
        System.out.println (ownerList.size ()+"999");
        if(ownerList.size ()==0){
            System.out.println (url+"url999");
              Owner owner=new Owner ();
               owner.setOwnerImg (url);
               owner.setStatus ("待审核");
               owner.setUiid (uiid);
               ownerMapper.insertSelective (owner);
        }

    }
    @Override
    public void uploadPackageProperty(String url, Integer uiid) {
         ParkingExample parkingExample=new ParkingExample ();
        ParkingExample.Criteria criteria = parkingExample.createCriteria ();
        criteria.andUiidEqualTo (uiid);
        List<Parking> parkings = parkingMapper.selectByExample (parkingExample);
        int i=(int)(Math.random()*900)+100;
        String s= String.valueOf (i);
        String pnumber= ("A")+"-"+s;
        System.out.println (pnumber);
        if(parkings.size ()==0){
            System.out.println (url+"照片路径");
             Parking parking=new Parking ();
             parking.setUiid (uiid);
             parking.setPnumber (pnumber);
             parking.setStatus ("待审核");
             parking.setImg (url);
            System.out.println (parking+"ppp");
             parkingMapper.insertSelective (parking);
        }

    }

    @Override
    public Map<String, Object> findByUiidAndPid(Integer uiid, Integer pageNum, Integer pageSize) {

        Map<String,Object> map=new HashMap<> ();

        PageHelper.startPage (pageNum,pageSize);
        List<Comments> comments = parkingMapper.selectByUiidComment (uiid);
        PageInfo<Comments> pageInfo=new PageInfo<> (comments);

        map.put ("comments",comments);
        map.put ("pageInfo",pageInfo);


        return map;
    }


}
